package com.tarena.lbs.business.dao.repository.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BusinessRepositoryImpl implements BusinessRepository {
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public PageInfo<BusinessPO> pageList(BusinessQuery query) {
        QueryWrapper<BusinessPO> queryWrapper =new QueryWrapper<>();
        if (StringUtils.isNotBlank(query.getBusinessName())){
            queryWrapper.like("business_name", query.getBusinessName());
        }
        if (StringUtils.isNotBlank(query.getBusinessHeadPhone())){
            queryWrapper.like("business_head_phone", query.getBusinessHeadPhone());
        }
        if (query.getBusinessStatus() != null){
            queryWrapper.eq("business_status", query.getBusinessStatus());
        }
        if (query.getStartingTime() != null){
            queryWrapper.gt("entry_time", query.getStartingTime());
        }
        if (query.getEndTime() != null){
            queryWrapper.lt("entry_time", query.getEndTime());
        }
        if (query.getBusinessId() != null){
            queryWrapper.eq("id", query.getBusinessId());
        }
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<BusinessPO> list = businessMapper.selectList(queryWrapper);
        return new PageInfo<>(list);
    }

    @Override
    public Long countByBusinessName(String businessName) {
        QueryWrapper<BusinessPO> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("business_name", businessName);
        return businessMapper.selectCount(queryWrapper);
    }

    @Override
    public void insert(BusinessPO businessPO) {
        businessMapper.insert(businessPO);
    }

    @Override
    public BusinessPO busiStoreDetail(Integer businessId) {

        return businessMapper.selectById(businessId);
    }

    @Override
    public void update(BusinessPO businessPO) {
        businessMapper.updateById(businessPO);
    }


}
